using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;


using NHibernate.Expression;
using Adge.Models;
using Adge.BLL;
using Adge.Common;
using Adge.Common.Collections;

namespace Adge.Web.UI.Admin
{
    public partial class CatFramecs : System.Web.UI.Page
    {
        protected string navURL = string.Empty;

        private string ObjCatname
        {
            get
            {
                string catname = string.Empty;

                if (!string.IsNullOrEmpty(Request["catname"]))
                {
                    catname = Request["catname"];
                }

                return catname;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                navURL = AdgeConfig.GetCatNameByIndex(ObjCatname).Value;
                InitTree();                
            }

        }

        private void InitTree()
        {
            AdgeArBase<CategoryInfo> bllSub = new AdgeArBase<CategoryInfo>();
            Order[] orders = new Order[] { };
            ICriterion[] parentCriterias = new ICriterion[] { new EqExpression("Catname", AdgeConfig.GetCatNameByIndex(ObjCatname).Name) };
            CategoryInfo[] parentSub = bllSub.SlicedFindAll(0, 100, orders, parentCriterias);
            IList<CategoryInfo> subs = new List<CategoryInfo>();
            if (parentSub.Length > 0 && parentSub[0].Children.Count > 0)
            {
                subs = parentSub[0].Children;
            }
            else
            {
                subs = Adapters.ArrayToList<CategoryInfo>(parentSub);
            }
            
            BindRootNode(subs);
        }

        private void BindRootNode(IList<CategoryInfo> catinfo)
        {
            foreach (CategoryInfo cat in catinfo)
            {
                if (cat.CatId > 0)
                {
                    TreeNode node = new TreeNode();
                    node.Text = TitleAdapter(cat.Catname, 6);
                    node.Value = cat.CatId.ToString();
                    node.ToolTip = cat.CatDesc;
                    node.Target = "newscenter";
                    node.NavigateUrl = AdgeConfig.GetCatNameByIndex(ObjCatname).Value + "&catid=" + cat.CatId.ToString();
                    tvCats.Nodes.Add(node);
                }
            }
        }

        protected string TitleAdapter(string title, int number)
        {
            if (title.Length > number)
            {
                return string.Concat(Utils.CutString(title, 0, number), "...");
            }
            else
            {
                return title;
            }
        }
    }
}
